package com.quktzhuanka.app.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.bean.JdGoodsResp;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.utils.VerticalImageSpan;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.DecimalFormat;
import java.util.List;

public class JDAdapterList extends CommonAdapter<JdGoodsResp> {
    SpannableString spannableString;
    Drawable drawable;
    DecimalFormat df = new DecimalFormat("0.00");
    private ShopRecyclerAdapter.OnDeleteClickLister mDeleteClickListener;

    public JDAdapterList(Context context, int layoutId, List<JdGoodsResp> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, final JdGoodsResp item, int position) {
        ImageView imageView = holder.getView(R.id.image);
//        if(item.getPicurls().equals("")||item.getImageInfo()[0].getImageList().length==0){
        if(item.getPicurls().equals("")){
            Glide.with(mContext).load("").placeholder(R.drawable.no_banner).dontAnimate().into(imageView);
        }else{
            Glide.with(mContext).load(item.getPicurl()).placeholder(R.drawable.no_banner).dontAnimate().into(imageView);
        }
        spannableString = new SpannableString("  " + item.getGoods_name());
        drawable = mContext.getResources().getDrawable(R.mipmap.label_jd);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        spannableString.setSpan(new VerticalImageSpan(drawable), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((TextView) holder.getView(R.id.title_child)).setText(spannableString);
        //券后2
        TextView tx2_2 = holder.getView(R.id.tx2_2);
        //设置删除线
        tx2_2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        tx2_2.setText("￥" + item.getPrice());
        //内部
        TextView tx3 = holder.getView(R.id.tx3);
//        if(item.getCouponInfo().length==0||item.getCouponInfo()[0].getCouponList().length==0){
//            tx3.setText(0 + "");
//        }else{
//        }
        tx3.setText("￥"+item.getDiscount());
        TextView tx2 = holder.getView(R.id.tx2);
        tx2.setText("￥" + df.format(Double.parseDouble(item.getPrice_after())));
//        try {
//            tx2.setText("￥" + df.format(item.getPriceInfo()[0].getPrice() - item.getCouponInfo()[0].getCouponList()[0].getDiscount()));
//        } catch (Exception e) {
//        }
        if(item.getCommission().equals('0')){
            holder.setText(R.id.tx4, "奖:0");
        }else{
            holder.setText(R.id.tx4, "奖:" + df.format(Double.parseDouble(item.getCommission())*Double.parseDouble(df.format((float) SPUtils.getIntData(mContext, "rate", 0) / 100))));
        }
        if (Integer.parseInt(item.getSales()) < 1000) {
            holder.setText(R.id.tx5, "销量:" + item.getSales());
        } else {
            holder.setText(R.id.tx5, "销量:" + df.format((float) Integer.parseInt(item.getSales()) / 10000) + "万");
        }
        holder.getView(R.id.tv_delete).setTag(position);
        holder.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDeleteClickListener != null) {
                    mDeleteClickListener.onDeleteClick(v, (Integer) v.getTag());
                }
            }
        });
    }
    public void setOnDeleteClickListener(ShopRecyclerAdapter.OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }
}
